home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / uu.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  4.2 KB  |  159 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. import binascii
  5. import os
  6. import string
  7. import sys
  8. Error = 'uu.Error'
  9.  
  10. def encode(in_file, out_file, name = None, mode = None):
  11.     '''Uuencode file'''
  12.     if in_file == '-':
  13.         in_file = sys.stdin
  14.     elif type(in_file) == type(''):
  15.         if name == None:
  16.             name = os.path.basename(in_file)
  17.         
  18.         if mode == None:
  19.             
  20.             try:
  21.                 mode = os.stat(in_file)[0]
  22.             except AttributeError:
  23.                 pass
  24.  
  25.         
  26.         in_file = open(in_file, 'rb')
  27.     
  28.     if out_file == '-':
  29.         out_file = sys.stdout
  30.     elif type(out_file) == type(''):
  31.         out_file = open(out_file, 'w')
  32.     
  33.     if name == None:
  34.         name = '-'
  35.     
  36.     if mode == None:
  37.         mode = 438
  38.     
  39.     out_file.write('begin %o %s\n' % (mode & 511, name))
  40.     str = in_file.read(45)
  41.     while len(str) > 0:
  42.         out_file.write(binascii.b2a_uu(str))
  43.         str = in_file.read(45)
  44.     out_file.write(' \nend\n')
  45.  
  46.  
  47. def decode(in_file, out_file = None, mode = None):
  48.     '''Decode uuencoded file'''
  49.     if in_file == '-':
  50.         in_file = sys.stdin
  51.     elif type(in_file) == type(''):
  52.         in_file = open(in_file)
  53.     
  54.     while 1:
  55.         hdr = in_file.readline()
  56.         if not hdr:
  57.             raise Error, 'No valid begin line found in input file'
  58.         
  59.         if hdr[:5] != 'begin':
  60.             continue
  61.         
  62.         hdrfields = string.split(hdr)
  63.         if len(hdrfields) == 3 and hdrfields[0] == 'begin':
  64.             
  65.             try:
  66.                 string.atoi(hdrfields[1], 8)
  67.             except ValueError:
  68.                 pass
  69.  
  70.         
  71.     if out_file == None:
  72.         out_file = hdrfields[2]
  73.     
  74.     if mode == None:
  75.         mode = string.atoi(hdrfields[1], 8)
  76.     
  77.     if out_file == '-':
  78.         out_file = sys.stdout
  79.     elif type(out_file) == type(''):
  80.         fp = open(out_file, 'wb')
  81.         
  82.         try:
  83.             os.path.chmod(out_file, mode)
  84.         except AttributeError:
  85.             pass
  86.  
  87.         out_file = fp
  88.     
  89.     s = in_file.readline()
  90.     while s and s != 'end\n':
  91.         
  92.         try:
  93.             data = binascii.a2b_uu(s)
  94.         except binascii.Error:
  95.             v = None
  96.             nbytes = ((ord(s[0]) - 32 & 63) * 4 + 5) / 3
  97.             data = binascii.a2b_uu(s[:nbytes])
  98.             sys.stderr.write('Warning: %s\n' % str(v))
  99.  
  100.         out_file.write(data)
  101.         s = in_file.readline()
  102.     if not str:
  103.         raise Error, 'Truncated input file'
  104.     
  105.  
  106.  
  107. def test():
  108.     '''uuencode/uudecode main program'''
  109.     import getopt
  110.     dopt = 0
  111.     topt = 0
  112.     input = sys.stdin
  113.     output = sys.stdout
  114.     ok = 1
  115.     
  116.     try:
  117.         (optlist, args) = getopt.getopt(sys.argv[1:], 'dt')
  118.     except getopt.error:
  119.         ok = 0
  120.  
  121.     if not ok or len(args) > 2:
  122.         print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]'
  123.         print ' -d: Decode (in stead of encode)'
  124.         print ' -t: data is text, encoded format unix-compatible text'
  125.         sys.exit(1)
  126.     
  127.     for o, a in optlist:
  128.         if o == '-t':
  129.             topt = 1
  130.         
  131.     
  132.     if len(args) > 0:
  133.         input = args[0]
  134.     
  135.     if len(args) > 1:
  136.         output = args[1]
  137.     
  138.     if dopt:
  139.         if topt:
  140.             if type(output) == type(''):
  141.                 output = open(output, 'w')
  142.             else:
  143.                 print sys.argv[0], ': cannot do -t to stdout'
  144.                 sys.exit(1)
  145.         
  146.         decode(input, output)
  147.     elif topt:
  148.         if type(input) == type(''):
  149.             input = open(input, 'r')
  150.         else:
  151.             print sys.argv[0], ': cannot do -t from stdin'
  152.             sys.exit(1)
  153.     
  154.     encode(input, output)
  155.  
  156. if __name__ == '__main__':
  157.     test()
  158.  
  159.